package com.meteor.controller;

import com.github.pagehelper.PageInfo;
import com.meteor.pojo.User;
import com.meteor.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class UserController {

    @Resource
    private UserService userService;
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd yy:MM:ss");

    @GetMapping("/login")
    public String login() {
        return "back/login";
    }

    // 进入注册页面
    @GetMapping("/register")
    public String register() {
        return "back/register";
    }
    @PostMapping("/addRegister")
    public String addRegister(User user,Model model) {
        user.setIdentity("普通用户");
        user.setMoney(0.0);
        user.setCreateTime(new Date());
        String pwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(pwd);

        User user1 = userService.selectUser(user.getUsername());
        if(user1 != null) {
            model.addAttribute("message", "添加失败，用户名重复！");
            return "back/login";
        }

        int i = userService.insertUser(user);
        if( i > 0) {
            model.addAttribute("message", "注册成功，请登录!");
        }else {
            model.addAttribute("message", "注册失败，操作有误！");
        }
        return "back/login";
    }


    // 用户登录判断
    @PostMapping("/login")
    public String login(String username, String password, HttpSession session, Model model) {
        // 将密码进行md5加密
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        User user = userService.selectUserByUsernameAndPwd(username, pwd);
        System.out.println(pwd);
        if (user == null) {
            // 如果输入错误，则返回错误信息
            model.addAttribute("message", "用户名或密码错误!");
            return "back/login";
        }
        session.setAttribute("user", user);
        if (user.getIdentity().equals("普通用户")) {
            // 0代表普通用户
            return "redirect:/admin/userCenter";
        }
        return "back/welcome";
    }

    // 跳到用户管理页面
    @GetMapping("/user")
    public String user(Model model) {
        PageInfo<User> users = userService.selectAll(1, null);
        List<User> list = users.getList();
        model.addAttribute("users",list);
        model.addAttribute("info", users);
        return "back/admin";
    }


    // 条件查询加分页
    @RequestMapping("/userPage")
    @ResponseBody
    public PageInfo<User> user(Integer pageNum,String username) {
        if(username.equals("")) {
            username = null;
        }
        PageInfo<User> users = userService.selectAll(pageNum, username);
        return users;
    }

    // 用户新增
    @GetMapping("/insert")
    @Transactional
    public String insertUser(User user,Model model) {
        Date date = new Date();
        user.setCreateTime(date);
        String pwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(pwd);
        User user1 = userService.selectUser(user.getUsername());
        if(user1 != null) {
            model.addAttribute("message", "添加失败，用户名重复！");
            return "forward:/admin/user";
        }
        if(user.getIdentity().equals("管理员") || user.getIdentity().equals("普通用户")) {
            int i = userService.insertUser(user);
            if (i > 0) {
                model.addAttribute("message", "新增成功");
            } else {
                model.addAttribute("message", "新增失败");
            }
            return "forward:/admin/user";
        }
        model.addAttribute("message", "添加失败，身份类型只有管理员或普通用户！");
        return "forward:/admin/user";
    }

    // 用户删除
    @GetMapping("/delete")
    public String delete(Integer id,Model model) {
        int i = userService.deleteUserById(id);
        PageInfo<User> users = userService.selectAll(1, null);
        List<User> list = users.getList();
        model.addAttribute("users",list);
        model.addAttribute("info", users);
        model.addAttribute("message", "删除成功");
        return "back/admin::tab";
    }

    // 用户修改
    @GetMapping("/update")
    public String update(User user,Model model) {
        Date date = new Date();
        user.setCreateTime(date);
        String pwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(pwd);
        if(user.getIdentity().equals("管理员") || user.getIdentity().equals("普通用户")) {
            int i = userService.updateUserById(user);
            model.addAttribute("message", "修改成功");
        }else {
            model.addAttribute("message", "修改失败，身份只能是管理员或者普通用户");
        }
        PageInfo<User> users = userService.selectAll(1, null);
        List<User> list = users.getList();
        model.addAttribute("users",list);
        model.addAttribute("info", users);
        return "back/admin::tab";
    }

//    ----------------------------------------------前端用户中心-------------------------------------------------------
    // 跳到用户中心页面
    @GetMapping("/userCenter")
    public String selectUserCenterByName(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        User user1 = userService.selectUser(user.getUsername());
        request.setAttribute("user", user1);
        return "front/userCenter";
    }

    // 修改用户自己信息
    @PostMapping("/updateUserByUserName")
    public String updateUserByUserName(String username, String password,HttpServletRequest request) {
        User user =(User) request.getSession().getAttribute("user");
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());

        User user1 = userService.selectUser(username);
        if(user1 != null) {
            request.setAttribute("message", "修改失败，用户名重复！");
            return "front/userCenter";
        }
        int i = userService.updateUserByUserName(username, pwd, user.getUsername());
        if (i > 0) {
            User user2 = userService.selectUser(username);
            request.setAttribute("user", user2);
            request.setAttribute("message", "个人信息修改成功！");
        }else {
            request.setAttribute("message", "个人信息修改失败！");
        }
        return "front/userCenter";
    }

    // 用户充值
    @GetMapping("/updateUserMoney")
    @Transactional
    public String updateUserMoney(Double money,HttpServletRequest request) {
        User user =(User) request.getSession().getAttribute("user");
        int i = userService.updateUserMoney(money, user.getUsername());
        if (i > 0) {
            User user1 = userService.selectUser(user.getUsername());
            request.setAttribute("user", user1);
            request.setAttribute("message", "余额充值成功！");
        }else {
            request.setAttribute("message", "余额充值失败！");
        }
        return "front/userCenter";
    }
}













